package com.rado.tf.gui;

import com.vaadin.server.UIClassSelectionEvent;
import com.vaadin.server.UICreateEvent;
import com.vaadin.server.UIProvider;
import com.vaadin.ui.UI;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by parado on 14.03.14.
 */
public class MainUIProvider extends UIProvider
{
   @Override
   public Class<? extends UI> getUIClass(UIClassSelectionEvent event)
   {
      return MainUI.class;
   }

   @Override
   public UI createInstance(UICreateEvent event)
   {
      try
      {
         MainUI ui = (MainUI) event.getUIClass().newInstance();
         ConfigurableApplicationContext uiCtx = new ClassPathXmlApplicationContext(
                 event.getService().getDeploymentConfiguration().getInitParameters().getProperty(
                         "contextConfigLocation"));
         ui.setUiContext(uiCtx);
         return ui;
      }
      catch (InstantiationException e)
      {
         throw new RuntimeException("Could not instantiate UI class", e);
      }
      catch (IllegalAccessException e)
      {
         throw new RuntimeException("Could not access UI class", e);
      }
   }
}
